<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRole"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="roleList">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="assignRole(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑 </el-button>
                  <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
                </template>

              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination layout="prev,pager,next" :page-size="page.pagesize" :total="total" @current-change="currentChange" />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyInfo.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyInfo.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyInfo.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>
      </el-card>
      <el-dialog :ref="roleForm" :title="roleForm.id?'编辑角色':'添加角色'" :visible="showDialog" @close="btnCancel">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

      <!-- 权限管理弹窗 -->
      <el-dialog :visible="showPerm" title="分配权限" @close="btnPermCancel">
        <el-tree ref="permTree" :data="permissionList" :props="{label:'name'}" default-expand-all show-checkbox check-strictly node-key="id" />
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getCompanyInfo, getRoleList, delRole, addRole, editRole, getRoleDetail, assignPerm } from '@/api/setting'
import { getPermissionList } from '@/api/permission'
import { listToTreeData } from '@/utils'
export default {
  data() {
    return {
      companyInfo: {}, // 公司信息
      // 获取角色列表参数
      page: {
        page: 1,
        pagesize: 5
      },
      total: 0, // 角色列表总条数
      roleList: [], // 角色列表
      showDialog: false,
      showPerm: false, // 权限弹窗
      permissionList: [], // 权限列表
      roleId: '',
      // 弹窗内的数据
      roleForm: {
        name: '',
        description: '',
        id: '',
        permIds: ''
      },
      rules: {
        name: [{ required: true, message: '该项不能为空', trigger: 'blur' }, { max: 10, message: '内容长度不能超过10位', trigger: 'blur' }],
        description: [{ required: true, message: '该项不能为空', trigger: 'blur' }, { max: 50, message: '内容长度不能超过50位', trigger: 'blur' }]
      }
    }
  },
  created() {
    // 获取公司信息
    this.getCompanyInfo()
    // 获取角色列表
    this.getRoleList()
    this.permList()
  },
  methods: {
    // 获取公司信息
    async getCompanyInfo() {
      this.companyInfo = await getCompanyInfo(this.$store.state.user.companyId)
    },
    // 获取角色列表
    async getRoleList() {
      const res = await getRoleList(this.page)
      this.roleList = res.rows
      this.total = res.total
      // console.log(res.rows)
    },
    // 设置分页功能
    currentChange(currentPage) {
      this.page.page = currentPage
      this.getRoleList()
    },
    async  delRole(id) {
      try {
        await this.$confirm('确定删除吗？')
        await delRole(id)
        if (this.roleList.length === 1 && this.page.page > 1) {
          this.page.page--
        }
        this.$message.success('删除成功！')
        // 刷新列表数据
        this.getRoleList()
      } catch (error) {
        console.log(error)
      }
    },
    // 新增角色
    async  addRole() {
      this.showDialog = true
    },
    // 编辑角色
    async editRole(id) {
      this.roleForm = await getRoleDetail(id)
      this.showDialog = true
    },

    // 弹窗确定功能
    async btnOK() {
      // 校验表单
      this.$refs.roleForm.validate()
      // 判定是否编辑或新增
      if (this.roleForm.id) {
        // 编辑
        await editRole(this.roleForm)
        this.$message.success('编辑成功！')
      } else {
        // 新增
        await addRole(this.roleForm)
        this.$message.success('添加成功！')
      }

      // 刷新列表数据
      this.getRoleList()
      // 关闭弹窗
      this.showDialog = false
    },
    btnCancel() {
      // 关闭弹窗
      this.showDialog = false
      // 清理校验报错
      this.$refs.roleForm.resetFields()
      // 清空数据
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    async permList() {
      this.permissionList = listToTreeData(await getPermissionList(), '0')
    },
    // 权限管理
    async assignRole(id) {
      this.showPerm = true
      this.roleId = id
      const { permIds } = await getRoleDetail(id)
      this.$nextTick(() => {
        this.$refs.permTree.setCheckedKeys(permIds)
      })
    },
    //
    async  btnPermOK() {
      // 发送请求
      await assignPerm({ id: this.roleId, permIds: this.$refs.permTree.getCheckedKeys() })
      // 提醒用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.showPerm = false
    },
    btnPermCancel() {
      this.showPerm = false
    }

  }
}
</script>

<style>

</style>
